/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Animation.Animations;

import Scenery.BaseSprite;
import main.DumbMath;
import com.jme3.math.Matrix3f;
import com.jme3.math.Quaternion;
import com.jme3.math.Vector3f;
import Animation.Animation;
import Animation.Animation;
import Scenery.BaseCharacter;
import Scenery.BaseSprite;
import Scenery.Thing.Weapons.Weapon;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Ray;
import com.jme3.scene.debug.Arrow;
import main.DumbMath;
import main.Main;

/*
 * 
 * @author: Ziden
 * 
 */

public class Attack extends Animation {

    public Attack(BaseCharacter dude) {
        super(dude, 20);
        buildAnimation();
    }
    
    private Weapon weapon;

    // RHAND
    private Vector3f hand;
    private float handRot;
    private float handSpeed = 0.1f;
    
    // BODY
    private Quaternion startRot;
    private float bodyRot = 0f;
    private float bodyRotSpeed = 0.13f;

    private float weaponSpeed = 0.25f;
    private float weaponRotation = 0f;

    @Override
    public void gotoInitialState() {
        animationBody.getRhand().setLocalTranslation(hand);
        animationBody.getBodyNode().setLocalRotation(startRot);
        bodyRot = 0;
        handRot = 0;
        canceled = false;
        //weapon = null;
        parameter = null;
        frame = 0;
    }

    @Override
    public String getName() {
        return "Attack";
    }

    @Override
    public void saveInitialState() {;
        startRot = animationBody.getBodyNode().getLocalRotation().clone();
        hand = animationBody.getRhand().getLocalTranslation().clone();
    }

    @Override
    public void setParameter(Object p) {
        if(p instanceof Weapon)
            weapon = (Weapon)p;
        parameter = p;
    }

    
    @Override
    protected void buildFrame() {
       //if(frame==0)
       //    bodyRot = -5;
       //else
       //    bodyRot = 5/10;
       bodyRot = 0.3f/2;
       animationFrames[frame][0] = bodyRot;
    }

    @Override
    public void performFrame() {
        if(frame==maxFrame/2) {
            Ray r = new Ray();
            r.setOrigin(animationBody.getControl().getPhysicsLocation());
            r.setDirection(animationBody.getLookDir().setY(0));
            r.setLimit(20);
            animationBody.checkContact(r);
        }
        weapon.getMasterWeaponNode().setLocalRotation(DumbMath.PITCH090);
        animationBody.getBodyNode().rotate(0, animationFrames[frame][0], 0);
        if(frame==maxFrame-1) {
            canceled = true; 
            weapon.getMasterWeaponNode().setLocalRotation(DumbMath.PITCH360);
        }
    }

    @Override
    protected float getCicleRunTime() {
        return 0f;
    }
}
